package com.leoandroid.tool.toolsbox.net

import okhttp3.*
import org.json.JSONException
import org.json.JSONObject
import java.io.IOException

class NetRequest {

    companion object {

        fun asyncGet(url: String, callBack: NetCallBack) {
            //创建request请求对象
            val request = Request.Builder()
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .url(url)
                //.method()方法与.get()方法选取1种即可
                .method("GET", null)
                .build()

            //创建call并调用enqueue()方法实现网络请求
            OkHttpClient().newCall(request)
                .enqueue(object : Callback {
                    override fun onFailure(call: Call, e: IOException) {
                        callBack.onFailure(-100, e)
                    }

                    override fun onResponse(call: Call, response: Response) {
                        val result = response.body!!.string()
                        try {
                            val jsonObject = JSONObject(result)
                            callBack.onSuccess(jsonObject)
                        } catch (e: JSONException) {
                            e.printStackTrace()
                        }
                    }
                })
        }

        fun asyncGet2(url: String, callBack: NetCallBack2) {
            //创建request请求对象
            val request = Request.Builder()
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .addHeader("Accept", "application/json")
                .url(url)
                .method("GET", null)
                .build()

            //创建call并调用enqueue()方法实现网络请求
            OkHttpClient().newCall(request)
                .enqueue(object : Callback {
                    override fun onFailure(call: Call, e: IOException) {
                        callBack.onFailure(-100, e)
                    }

                    override fun onResponse(call: Call, response: Response) {
                        val result = response.body!!.string()
                        callBack.onSuccess(result)
                    }
                })
        }


        fun asyncPost(requestBody: RequestBody, url: String, callBack: NetCallBack2) {
            //创建request请求对象
            val request = Request.Builder()
                .url(url)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .addHeader("Accept", "application/json")
                .post(requestBody)
                .build()

            //创建call并调用enqueue()方法实现网络请求
            OkHttpClient().newCall(request)
                .enqueue(object : Callback {
                    override fun onFailure(call: Call, e: IOException) {
                        callBack.onFailure(-100, e)
                    }

                    override fun onResponse(call: Call, response: Response) {
                        val result: String = response.body!!.string()
                        callBack.onSuccess(result)
                    }
                })
        }
    }
}